package work.xiaohong.arithmetic.IO;

import java.io.*;

/**
 * @author xiaohong
 * @version 1.0
 * @date 2021/10/27 0027 10:49
 * @description 普通字节流
 */
public class FileStream {

    public static void main(String[] args) throws Exception {
        writeFile();
//        readFile();
//        copyFile();
    }

    // 写入文件
    public static void writeFile() throws Exception {
        // 后面的参数表示是否要子在原来的内容后面追加新内容
        FileOutputStream fileOutputStream = new FileOutputStream("a.txt",true);
        for (int i = 10000; i > 0; i--) {
            fileOutputStream.write(String.valueOf(i+"\n").getBytes());
        }
        fileOutputStream.close();
    }


    // 读取文件
    public static void readFile() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = fileInputStream.read(buffer)) != -1){
            System.out.println(new String(buffer,0,count));
        }
        fileInputStream.close();
    }


    // 复制文件
    public static void copyFile() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("b.txt",true);
        byte[] buffer = new byte[1024];
        int count = 0;
        while ((count = fileInputStream.read(buffer)) != -1){
            fileOutputStream.write(buffer,0,count);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }


}
